package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CarListInfoBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.gongneng.car.SelectCarActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

public class ApplyCarActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;

    private String msg;
    @ToSave
    CarListInfoBean carListInfoBean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "用车申请");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.APPLY_CAR)
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("vehicleId", carListInfoBean.getId())
                        .params("plateNumber", carListInfoBean.getPlateNumber())
                        .params("applyer", AppConstent.user.getGuid())
                        .params("applyerName", AppConstent.user.getUsername())
                        .params("applyerDepId", AppConstent.user.getDepartId())
                        .params("applyerDepName", AppConstent.user.getDepartment())
                        .params("startTime", setParam(2))
                        .params("endTime", setParam(3))
                        .params("origin", setParam(4))
                        .params("destination", setParam(5))
                        .params("theDriverName", setParam(6))
                        .params("personCount", setParam(7))
                        .params("theReason", setParam(8))
                        .params("theCustomer", setParam(9))
                        .params("theProject", setParam(10))
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParams() {
        for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
            if (slimAdapterEx.getDataItem(i).isNeed()) {
                if (isParamBlank(i)) {
                    msg = "请输入" + slimAdapterEx.getDataItem(i).getTitle().replace("*", "");
                    return false;
                }
            }
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                startActivityForResult(SelectCarActivity.class, null, Config.SELECT_CAR);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditApplyTitleBean());
        datas.add(new SlimEditEnterBean("*车", "", 1, true, "请选择").setNeed(true));
        datas.add(new SlimEditTimeBean("*用车开始时间", "", 2, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE).setNeed(true));
        datas.add(new SlimEditTimeBean("*用车结束时间", "", 3, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE).setNeed(true));
        datas.add(new SlimEditEnterBean("*始发地", "", 4, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*目的地", "", 5, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*司机姓名", "", 6, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*坐车人数", "", 7, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("*申请事由", "", 8, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("关联客户", "", 9, "请输入"));
        datas.add(new SlimEditEnterBean("关联项目", "", 10, "请输入"));
        datas.add(new SlimEditHintBean("审批人", "", 11));
    }

    @Override
    protected void loadNetData() {

        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<String>>(ApplyCarActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(slimAdapterEx.getData().size() - 1).setContent(httpResult.getItems());
                            slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().size() - 1);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.SELECT_CAR && resultCode == Config.SUCCESS) {
            carListInfoBean = (CarListInfoBean) data.getSerializableExtra(Config.BEAN);
            String startTime = data.getStringExtra(Config.START_TIME);
            String endTime = data.getStringExtra(Config.END_TIME);
            slimAdapterEx.getDataItem(2).setContent(startTime);
            slimAdapterEx.getDataItem(3).setContent(endTime);
            slimAdapterEx.getDataItem(1).setContent(carListInfoBean.getVehicleType() + "-" + carListInfoBean.getPlateNumber());
            slimAdapterEx.notifyItemRangeChanged(1, 3);
        }
    }
}
